//
//  HLIMKitMessageTextContentView.m
//  YueYue
//
//  Created by hulu001 on 2024/1/12.
//

#import "HLIMKitMessageTextContentView.h"
#import "HLChatKit.h"
#import "M80AttributedLabel+HLIMKit.h"

@interface HLIMKitMessageTextContentView () <M80AttributedLabelDelegate>
@property (strong,  nonatomic) UILabel *transLabel;
@property (strong,  nonatomic) UILabel *pinLabel;
@property (strong,  nonatomic) UIImageView *transIcon;

@end

@implementation HLIMKitMessageTextContentView


- (instancetype)init
{
    self = [super init];
    
    if (self) {
        M80AttributedLabel *contentLabel = [[M80AttributedLabel alloc] initWithFrame:CGRectZero];
        contentLabel.backgroundColor = UIColor.clearColor;
        contentLabel.delegate = self;
        contentLabel.linkColor = [UIColor qmui_colorWithHexString:@"#2E52A3"];
        contentLabel.font = [UIFont systemFontOfSize:16.f];
        [self addSubview:contentLabel];
        self.contentLabel = contentLabel;
        
        self.transLabel = [UILabel.alloc init];
        self.transLabel.textColor = HLTextColor1;
        self.transLabel.font = [UIFont systemFontOfSize:12.f];
        self.transLabel.numberOfLines = 0;
        [self addSubview:self.transLabel];
        
        self.pinLabel = [UILabel.alloc init];
        self.pinLabel.backgroundColor = UIColor.clearColor;
        self.pinLabel.font = [UIFont systemFontOfSize:16.f];
        self.pinLabel.numberOfLines = 0;
        [self addSubview:self.pinLabel];
        
        self.transIcon = [UIImageView.alloc init];
        self.transIcon.bounds = CGRectMake(0, 0, 16, 16);
        self.transIcon.image = [UIImage imageNamed:@"imTranslate"];
        [self addSubview:self.transIcon];
    }
    
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    UIEdgeInsets contentInsets = self.messageModel.contentViewInsets;
    
    CGFloat textSizeHeight = [self.messageModel.message.localExt[@"originTextHeight"] floatValue];
    CGFloat transSizeHeight = [self.messageModel.message.localExt[@"transSizeHeight"] floatValue];
    CGFloat pinSizeHeight  = [self.messageModel.message.localExt[@"pinSizeHeight"] floatValue];
    
    CGFloat tableViewWidth = self.superview.width;
    CGSize contentsize     = [self.messageModel contentSize:tableViewWidth];
    
    CGRect labelFrame = CGRectMake(contentInsets.left, contentInsets.top, contentsize.width, textSizeHeight);
    self.contentLabel.frame = labelFrame;
    
    CGFloat dY = contentInsets.top + textSizeHeight;
    
    if (transSizeHeight > 0) {
        dY += 8;
        self.transLabel.frame = CGRectMake(24 + 8, dY, contentsize.width - 8, transSizeHeight);
        self.transIcon.left = contentInsets.left;
        self.transIcon.centerY = self.transLabel.centerY;
    }
    
    if (pinSizeHeight) {
        dY += 9;
        self.pinLabel.frame = CGRectMake(contentInsets.left, dY, contentsize.width, pinSizeHeight);
    }
}

- (void)refreshData:(HLIMKitMessageModel *)messageModel
{
    [super refreshData:messageModel];
    
    HLIMKitMessageSetting *setting = [HLChatKit.shareSDK.config setting:messageModel.message];
    NIMMessage * message = messageModel.message;
    
    self.contentLabel.textColor = setting.textColor;
    self.contentLabel.font = setting.font;
    
    self.pinLabel.textColor = setting.textColor;
    self.pinLabel.font = setting.font;
    
    if (message.isOutgoingMsg) {
        NSString *c = message.localExt[@"originContent"];
        if (c) {
            [self.contentLabel im_setText:c];
        } else {
            [self.contentLabel im_setText:message.text];
        }
    } else {
        [self.contentLabel im_setText:message.text];
    }
    
    NSString *trans = message.localExt[@"trans"];
    if (trans.length) {
        self.transLabel.text = trans;
        self.transLabel.hidden = NO;
        self.transIcon.hidden = NO;
    } else {
        self.transLabel.hidden = YES;
        self.transIcon.hidden = YES;
    }
    
    NSString *pin = message.localExt[@"pin"];
    if (pin.length) {
        self.pinLabel.text = pin;
        self.pinLabel.hidden = NO;
    } else {
        self.pinLabel.hidden = YES;
    }
    
//    [self.contentLabel im_setText:message.text];
}

//- (void)onTouchDown:(id)sender
//{
//
//}

- (void)onTouchUpInside:(id)sender
{
    
}

- (void)onTouchUpOutside:(id)sender
{
    
}

- (void)m80AttributedLabel:(nonnull M80AttributedLabel *)label clickedOnLink:(nonnull id)linkData {
    //TODO:富文本超链接
    
    HLIMKitEvent *event = [HLIMKitEvent.alloc init];
    event.gestureName = HLIMKitGestureTouchUpInside;
    event.messageModel = self.messageModel;
    event.data = linkData;
    [self.delegate onTap:event];
}


@end
